package com.worklog.service.impl;


import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;

import com.base.service.impl.ServiceImpl;
import com.system.pojo.Person;
import com.worklog.pojo.Worklog;
import com.worklog.service.IWorklogService;

public class WorklogServiceImpl<T extends Worklog> extends ServiceImpl<T> implements
		IWorklogService<T> {

	@Override
	public boolean create(T worklog) {
		Integer flag = 0; //0表示 日志更新,1表示日志新增；
		String sql= "from Worklog w where w.person ="+ worklog.getPerson().getId()+" and w.date = '"+worklog.getDate()+"'";
		List<Worklog> list = dao.createQuery(sql).list();
		if(list.size()>0)
		{
			T log = (T) list.get(0);
			log.setContent(worklog.getContent());
			//BeanUtils.copyProperties(worklog,log);
			dao.save(log);
		return true;	
		}
		else
			{dao.create(worklog);
			return true;	
			}
	}
	
	public Worklog findByProperty(String propertyName, Object value){
		Worklog worklog =null;
		worklog = (Worklog) dao.list("from Worklog w where w."+ propertyName+ "= ?");
		return worklog;		
	}
	

	public T getWorklog(Person person, Date date) {
		
		List<T> list =dao.list("from Worklog w where w.person = "+ person.getId()+ " and w.date = '"+date+"'");
		if (list.size() > 0)
			return list.get(0);
		return null;
	}
}
